﻿using CodingBlog.Core.Model;
using CodingBlog.Core.Repository.Contract;
using CodingBlog.Core.Service;

using Moq;

using NUnit.Framework;

namespace CodingBlog.Core.Tests.Service
{
    [TestFixture]
    class BlogServiceTests
    {
        [Test]
        public void can_get_default_blog()
        {
            var testTitle = "test blog title";
            var testId = 1;

            var blog = GetFakeBlog(testId, testTitle);

            var blogRepositoryMock = new Mock<IBlogRepository>();
            blogRepositoryMock.Setup(repo => repo.GetDefaultBlog()).Returns(blog);
            
            var result = new BlogService(blogRepositoryMock.Object).GetDefaultBlog();

            Assert.NotNull(result);
            Assert.AreEqual(testId, result.Id);
            Assert.AreEqual(testTitle, result.Title);
        }

        [Test]
        public void can_add_new_blog()
        {
            var testTitle = "test blog title";
            var testId = 0;

            var blogToCreate = GetFakeBlog(testId, testTitle);

            var blogRepositoryMock = new Mock<IBlogRepository>();
            blogRepositoryMock.Setup(repo => repo.CreateBlog(blogToCreate)).Returns(blogToCreate);

            var blogCreated = new BlogService(blogRepositoryMock.Object).CreateBlog(blogToCreate);

            blogRepositoryMock.Verify(repo => repo.CreateBlog(blogToCreate), Times.Once());

            Assert.NotNull(blogCreated);
            Assert.AreEqual(testTitle, blogCreated.Title);
        }

        [Test]
        public void can_check_blog_count()
        {
            var mockBlogsCount = 7;
            var blogRepositoryMock = new Mock<IBlogRepository>();
            blogRepositoryMock.Setup(repo => repo.GetBlogsCount()).Returns(mockBlogsCount);

            var result = new BlogService(blogRepositoryMock.Object).GetBlogsCount();

            Assert.AreEqual(mockBlogsCount, result);
        }

        private Blog GetFakeBlog(int id, string title)
        {
            return new Blog {Id = id, Title = title};
        }
    }
}
